/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include <stack>

#include "物体类JIT函数.h"
#include "Context/数据创建销毁.h"

#include "物体/物体.h"

struct S_实例对象管理 {
	std::stack<S_物体*> m_物体池;

	S_实例对象管理() {
	}
	~S_实例对象管理() {
	}

	S_物体* f_添加(S_物体* 父, S_OBArray* 新物体, uint32 num) {
		
		for (uint32 i = f_ob_实例数量(父); i < num; ++i) {
			if (m_物体池.size()) {
				f_core_array_push_back((S_Array*)新物体, (uint8*)m_物体池.top());
				m_物体池.pop();
			}
			else {
				auto newOb = f_ob_创建实例(父);
				f_core_array_push_back((S_Array*)新物体, (uint8*)&newOb);
			}
		}
		
		return f_ob_创建实例(父);
	}
	void f_删除(S_OBArray* 物体集, uint32 留下数量) {
		for (uint32 i = 留下数量; i < 物体集->count; ++i) {
			f_ob_从父物体中移除(物体集->ptr_userData[i]);
			m_物体池.push(物体集->ptr_userData[i]);
		}
		f_core_array_resize((S_Array*)物体集, 留下数量);
	}
};

static S_实例对象管理 实例管理;


void f_JIT_Fun_创建物体实例(S_物体* 物体, S_OBArray* 新物体, uint32 数量) {
	实例管理.f_删除(新物体, 0);
	实例管理.f_添加(物体, 新物体, 数量);
	物体->m_isUpdate = true;
}

void f_JIT_Fun_创建变换实例(S_OBArray* 物体, uint32 数量) {
	数量 = DEF_Max(数量, 1);

	for (uint32 i = 0; i < 物体->count; ++i) {
		auto* ob = 物体->ptr_userData[i];
		
		f_ob_set实例数量(ob, 数量, false);

		for (uint32 k = 0; k < 数量; ++k) {
			f_ob_set颜色2(ob, ob->m_颜色, k, E_物体网格元素类型::e_MT_焦点);
			f_ob_set颜色2(ob, ob->m_颜色, k, E_物体网格元素类型::e_MT_面);
			f_ob_set颜色2(ob, ob->m_颜色, k, E_物体网格元素类型::e_MT_边);
			f_ob_set颜色2(ob, ob->m_颜色, k, E_物体网格元素类型::e_MT_框);
		}
	}
}



void f_node_物体类节点JIT函数初始化() {
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_ob_get变换矩阵), "Mat44f", "S_物体* ob", f_ob_get变换矩阵);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_ob_get实例变换矩阵), "Mat44f", "S_物体* ob, uint32 ID", f_ob_get实例变换矩阵);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_Fun_创建物体实例), DEF_S(void), "S_物体* 物体, S_OBArray* 新物体, uint32 数量", f_JIT_Fun_创建物体实例);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_Fun_创建变换实例), DEF_S(void), "S_OBArray* 物体, uint32 数量", f_JIT_Fun_创建变换实例);

	f_代码构建_添加JIT初始化函数指针(DEF_S(f_ob_load), DEF_S(void), "S_物体*, const S_Props&, const S_Props&, const S_Props&, S_设备环境& ", f_ob_load);
}




